package com.test.view;

import com.test.config.MyInfo;
import com.test.datas.LoginData;
import com.test.demo.R;
import com.test.http.HttpClientReceiver;
import com.test.http.HttpMethod;
import com.test.http.HttpMethodTag;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;


/**
 * 登录界面
 * @author Harry
 *
 */
@SuppressLint("HandlerLeak")
public class LoginActivity extends Activity implements OnClickListener,HttpClientReceiver {
	private static final int LOGIN_SUCC_MSG = 1;
	private static final int WRONG_INPUT_MSG = 2;
	private static final int NO_INTERNET_MSG = 3;
	private static final int INIT_MSG = 4;
	
	private Handler mHandler = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);		
		this.setContentView(R.layout.activity_login);		
		setOnClickListener();
		setMessageDeal();
	}
	
	//根据消息更新界面
	private void setMessageDeal(){
		if(mHandler != null){
			return;
		}
		
		mHandler = new Handler(){

			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				switch(msg.what){
				case LOGIN_SUCC_MSG:
					loginSuccessCallback();
					break;
				case WRONG_INPUT_MSG:
					wrongInputAlert();
					break;
				case NO_INTERNET_MSG:
					noInternetAlert();
					break;
				}
			}
			
		};
	}

	/**
	 * 初始化登录回调
	 */
	private void setOnClickListener(){
		Button loginButton = (Button)this.findViewById(R.id.login_layout_button);
		if(loginButton != null){
			loginButton.setOnClickListener(this);
		}
	}
	
	/**
	 * 登录成功回调
	 */
	private void loginSuccessCallback(){
		Intent intent = new Intent(LoginActivity.this,MainTabActivity.class);
		startActivity(intent);
	}


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.login_layout_button:
			doLoginAction();
			break;
		default:
			return;
		}
	}

	
	/**
	 * 获取输入内容
	 * @param id ： 控件ID
	 * @return ：控件输入内容
	 */
	private String getEditTextFromID(int id){		
		String text = "";
		EditText editText = (EditText)this.findViewById(id);
		if(editText != null){
			text = editText.getText().toString();
		}		
		return text;
	}
	
	/**
	 * 获取用户名和密码进行登录
	 */
	private void doLoginAction() {
		// TODO Auto-generated method stub
		String name     = getEditTextFromID(R.id.login_layout_user_id_editText);
		String password = getEditTextFromID(R.id.login_layout_password_editText);
		doLoginRequest(name,password);
	}
	
	/**
	 * 登录请求
	 * @param name ： 用户名
	 * @param password ： 密码
	 */
	private void doLoginRequest(String name,String password){		
		HttpMethod.login(this, name, password);
	}

	@Override
	public void onHttpResponse(int httpTag,String strJson) {
		// TODO Auto-generated method stub
		switch(httpTag){
		case HttpMethodTag.LOGIN://登录返回
			dealLoginResponse(strJson);			
			break;
		}
	}
	
	/**
	 * 数据请求返回响应
	 * @param strJson
	 */
	private void dealLoginResponse(String strJson){
		LoginData data = new LoginData();
		int parseResult = data.parseJson(strJson);
		Message msg = new Message();
		msg.what = INIT_MSG;
		switch(parseResult){
		case 0://用户名密码错误
			msg.what = WRONG_INPUT_MSG;			
			break;
		case 1://登录成功
			MyInfo.id = data.userName;
			msg.what = LOGIN_SUCC_MSG;			
			break;
		case -1://无网络提示
			msg.what = NO_INTERNET_MSG;			
			break;
		}
		if(msg.what != INIT_MSG){
			mHandler.sendMessage(msg);
		}
	}
	
	
	//显示用户名秘密错误提示框
	private void wrongInputAlert(){
		showAlertDiaglog(this.getResources().getString(R.string.wrong_login_info));
	}
	
	
	//显示网络连接失败提示框
	private void noInternetAlert(){		
		showAlertDiaglog(this.getResources().getString(R.string.net_link_error));
	}
	
	//显示提示框
	private void showAlertDiaglog(String msg){
		AlertDialog.Builder builder = new Builder(this);
		builder.setTitle(R.string.hint);
		builder.setMessage(msg);
		builder.setPositiveButton(R.string.ok, null);
		builder.create().show();
	}
}
